package org.example.main;

import java.util.List;
import java.util.Scanner;

/**
 * 系统入口类，包含主方法
 */
public class main {
    private static GradeManagementSystem System = new GradeManagementSystem();
    private static Scanner scanner = new Scanner(java.lang.System.in);

    public static void main(String[] args) {
        boolean running = true;

        while (running) {
            printMenu();
            int choice = scanner.nextInt();
            scanner.nextLine(); // 消耗换行符

            switch (choice) {
                case 1:
                    addClass();
                    break;
                case 2:
                    addStudent();
                    break;
                case 3:
                    addCourse();
                    break;
                case 4:
                    addGrade();
                    break;
                case 5:
                    viewStudentGrades();
                    break;
                case 6:
                    viewCourseGrades();
                    break;
                case 7:
                    viewStudentAverageScore();
                    break;
                case 8:
                    viewCourseAverageScore();
                    break;
                case 9:
                    running = false;
                    java.lang.System.out.println("退出系统，再见！");
                    break;
                default:
                    java.lang.System.out.println("无效选择，请重新输入！");
            }
        }

        scanner.close();
    }

    private static void printMenu() {
        java.lang.System.out.println("\n===== 学生成绩管理系统 =====");
        java.lang.System.out.println("1. 添加班级");
        java.lang.System.out.println("2. 添加学生");
        java.lang.System.out.println("3. 添加课程");
        java.lang.System.out.println("4. 添加成绩");
        java.lang.System.out.println("5. 查看学生成绩");
        java.lang.System.out.println("6. 查看课程成绩");
        java.lang.System.out.println("7. 查看学生平均分");
        java.lang.System.out.println("8. 查看课程平均分");
        java.lang.System.out.println("9. 退出系统");
        java.lang.System.out.println("请输入你的选择：");
    }

    private static void addClass() {
        java.lang.System.out.println("\n--- 添加班级 ---");
        java.lang.System.out.print("班级编号：");
        String classId = scanner.nextLine();
        java.lang.System.out.print("班级名称：");
        String className = scanner.nextLine();
        java.lang.System.out.print("班级地址：");
        String classLocation = scanner.nextLine();

        Class newClass = new Class(classId, className, classLocation);
        System.addClass(newClass);

        java.lang.System.out.println("班级添加成功！");
    }

    private static void addStudent() {
        java.lang.System.out.println("\n--- 添加学生 ---");
        java.lang.System.out.println ("学生编号：");
        String studentId = scanner.nextLine();
        java.lang.System.out.print("姓名：");
        String name = scanner.nextLine();
        java.lang.System.out.print("性别：");
        String gender = scanner.nextLine();
        java.lang.System.out.print("年龄：");
        int age = scanner.nextInt();
        scanner.nextLine(); // 消耗换行符
        java.lang.System.out.print("爱好：");
        String hobby = scanner.nextLine();

        Student newStudent = new Student(studentId, name, gender, age, hobby);
        system.addStudent(newStudent);

        java.lang.System.out.println("学生添加成功！");
    }

    private static void addCourse() {
        java.lang.System.out.println("\n--- 添加课程 ---");
        java.lang.System.out.print("课程编号：");
        String courseId = scanner.nextLine();
        java.lang.System.out.print("课程名称：");
        String courseName = scanner.nextLine();
        java.lang.System.out.print("前置知识：");
        String prerequisite = scanner.nextLine();

        Course newCourse = new Course(courseId, courseName, prerequisite);
        system.addCourse(newCourse);

        java.lang.System.out.println("课程添加成功！");
    }

    private static void addGrade() {
        java.lang.System.out.println("\n--- 添加成绩 ---");
        java.lang.System.out.print("学生编号：");
        String studentId = scanner.nextLine();
        Student student = system.getStudent(studentId);

        if (student == null) {
            java.lang.System.out.println("未找到该学生！");
            return;
        }

        java.lang.System.out.print("课程编号：");
        String courseId = scanner.nextLine();
        Course course = system.getCourse(courseId);

        if (course == null) {
            java.lang.System.out.println("未找到该课程！");
            return;
        }

        java.lang.System.out.print("成绩：");
        double score = scanner.nextDouble();
        scanner.nextLine(); // 消耗换行符

        Grade newGrade = new Grade(studentId, student.getName(), courseId, course.getCourseName(), score);
        system.addGrade(newGrade);

        java.lang.System.out.println("成绩添加成功！");
    }

    private static void viewStudentGrades() {
        java.lang.System.out.println("\n--- 查看学生成绩 ---");
        java.lang.System.out.print("学生编号：");
        String studentId = scanner.nextLine();

        Student student = system.getStudent(studentId);
        if (student == null) {
            java.lang.System.out.println("未找到该学生！");
            return;
        }

        List<Grade> grades = system.getGradesByStudent(studentId);

        if (grades.isEmpty()) {
            java.lang.System.out.println("该学生暂无成绩记录！");
        } else {
            java.lang.System.out.println("学生：" + student.getName() + " 的成绩：");
            for (Grade grade : grades) {
                java.lang.System.out.println(grade.getCourseName() + "：" + grade.getScore());
            }
        }
    }

    private static void viewCourseGrades() {
        java.lang.System.out.println("\n--- 查看课程成绩 ---");
        java.lang.System.out.print("课程编号：");
        String courseId = scanner.nextLine();

        Course course = system.getCourse(courseId);
        if (course == null) {
            java.lang.System.out.println("未找到该课程！");
            return;
        }

        List<Grade> grades = system.getGradesByCourse(courseId);

        if (grades.isEmpty()) {
            java.lang.System.out.println("该课程暂无成绩记录！");
        } else {
            java.lang.System.out.println("课程：" + course.getCourseName() + " 的成绩：");
            for (Grade grade : grades) {
                java.lang.System.out.println(grade.getStudentName() + "：" + grade.getScore());
            }
        }
    }

    private static void viewStudentAverageScore() {
        java.lang.System.out.println("\n--- 查看学生平均分 ---");
        java.lang.System.out.print("学生编号：");
        String studentId = scanner.nextLine();

        Student student = system.getStudent(studentId);
        if (student == null) {
            java.lang.System.out.println("未找到该学生！");
            return;
        }

        double averageScore = system.calculateAverageScoreByStudent(studentId);
        java.lang.System.out.println("学生：" + student.getName() + " 的平均分为：" + averageScore);
    }

    private static void viewCourseAverageScore() {
        java.lang.System.out.println("\n--- 查看课程平均分 ---");
        java.lang.System.out.print("课程编号：");
        String courseId = scanner.nextLine();

        Course course = system.getCourse(courseId);
        if (course == null) {
            java.lang.System.out.println("未找到该课程！");
            return;
        }

        double averageScore = system.calculateAverageScoreByCourse(courseId);
        java.lang.System.out.println("课程：" + course.getCourseName() + " 的平均分为：" + averageScore);
    }

    private static class GradeManagementSystem {
    }
}